package com.yycx.module.bus.provider.service.impl;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusCustomAddress;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.provider.mapper.BusCustomAddressMapper;
import com.yycx.module.bus.provider.service.BusCustomAddressService;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户收货地址接口实现类
 *
 * @author flyme
 * @date 2020-08-21
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusCustomAddressServiceImpl extends BaseServiceImpl<BusCustomAddressMapper, BusCustomAddress> implements BusCustomAddressService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusCustomAddress bcs, EntityMap extra) {
        bcs.setState(CommonConstants.ENABLED);
        return ResultBody.ok();
    }


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusCustomAddress> cq, BusCustomAddress bcs, EntityMap requestMap) {
        cq.eq(BusCustomAddress.class, "customCode", bcs.getCustomCode());
        cq.eq(BusCustomAddress.class, "addressType");
        cq.like(BusCustomAddress.class, "address");
        cq.orderByDesc("bca.createTime");
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeListEntityMap(CriteriaQuery<BusCustomAddress> cq, BusCustomAddress busCustomSh, EntityMap requestMap) {
        cq.eq(BusCustomAddress.class, "customId");
        cq.eq(BusCustomAddress.class, "addressType");
        cq.eq(true, "state", CommonConstants.ENABLED);
        cq.orderByDesc("createTime");
        return super.beforeListEntityMap(cq, busCustomSh, requestMap);
    }

    @Override
    public BusCustomAddress getByCustomCodeAndName(String customCode, String address, String linkName,Long companyId) {
        CriteriaQuery<BusCustomAddress> cq = new CriteriaQuery<>(BusCustomAddress.class);
        cq.eq(true, "customCode", customCode);
        cq.eq(true, "userName", linkName);
        cq.eq(true, "address", address);
        cq.eq(true, "companyId", companyId);
        return getOne(cq,false);
    }

    @Override
    public void deleteByCustomCode(String customCode,Long companyId) {
        CriteriaDelete<BusCustomAddress> cd = new CriteriaDelete<>();
        cd.eq(true, "customCode", customCode);
        cd.eq(true, "companyId", companyId);
        remove(cd);
    }
}
